<template>
	<om-addpage style="padding-top: 35px;" :menuName="menuName" @formSubmit="formSubmit" :busCode="busCode">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		errorModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		changeConfigAttr,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';

	export default {
		data() {
			return buildFormData({
				relateCache:'bro-jy-sampleBorrowingDelay-relate-cache',
				returnTime:''
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		provide() {
			return {
				formPage: this
			}
		},
		methods: {
			getWorkParam(){
				let _that = this
				return {
					borrowedSampleId:_that.$refs.autoform.formdata.borrowedSampleId
				}
			},
			codeInfoFunc(code){
				let _that = this
				_that.om.request(
					'sampleBorrowingDelay/getCodeInfo',
					{
						code:code
					},
					'post'
				).then(res=>{
					if(res.value){
						if(_that.subEditorObj['title2']){
							_that.subEditorObj['title2'].workList = res.value
						}
					}
				})
			},
			verifyCode(code){
				let _that = this
				_that.om.request(
					'sampleBorrowingReturn/verifyCode',
					{
						code:code
					}
				).then(res=>{
					if (res.succeed && res.value) {
						errorModal(_that,'提示信息',data.value, function () {
							_that.$refs.autoform.clear('title1',"relateId")
							_that.$refs.autoform.clear('title1',"productDelayNum")
							_that.$refs.autoform.clear('title1',"customerUserId")
							_that.$refs.autoform.clear('title1',"postId")
							_that.$refs.autoform.clear('title1',"instId")
							_that.$refs.autoform.clear('title1',"salesAttributeCategoryId")
							_that.$refs.autoform.clear('title1',"customerShowId")
							_that.$refs.autoform.clear('title1',"customerId")
							_that.$refs.autoform.clear('title1',"customerNameX")
							_that.$refs.autoform.clear('title1',"customerAddres")
							_that.$refs.autoform.clear('title1',"customerUser")
							_that.$refs.autoform.clear('title1',"customerMobile")
							_that.$refs.autoform.clear('title1',"currencyId")
							_that.$refs.autoform.clear('title1',"customerMoney")
							_that.$refs.autoform.clear('title1',"shortRemark")
						})
					} else {
						//借样事由
						_that.codeInfoFunc(code);
					}
				})
			},
			async getResult(code){
				let _that = this
				await _that.verifyCode(code)
			},
			initLoad(options) {
				let _that = this;
				var fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					{
						url:'currencyDict/getLocalCurrency',
					}
				]
				if(fileId){
					ajaxArr.push(
						{
							url:'sampleBorrowingDelay/getInfo',
							param:{
									id:fileId
								},
							method:'post'
						}
					)
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("topinfo","zposId","data",configSelectData(res[0],'id','posName'),mainData)
					changeEditor("title1",'relateId',{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({}),
							url:'sampleBorrowingDelay/getCodePage',
							namefield:'code',
							notefield:'customerUserName',
							cachename:_that.relateCache,
							searchfield:'q',
							initload:true
						}
					},null,mainData)
					if(_that.subEditorObj['title2']){
						let att = {
							getWorkParam:'getWorkParam'
						}
						let configData = _that.subConfigData['title2']
						if(hasEditor("title2",'newProductId',configData)){
							let title = getEditorTitle("title2",'newProductId',configData)
							att.titleTemplate = title+':<%= newProductCode || "无" %>'
						}
						changeEditor("title2",'dataStr',att,mainData)
					}
					changeConfigAttr("title2", "showButton", true,mainData)
					changeConfigAttr("title2", "buttonText", "赋值延期退还日期",mainData)
					changeConfigAttr("title2", "buttonLink","/pages/form/bro-jy-sampleBorrowingDelay/sampleBorrowingTemplate",mainData)
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push('id')
						for(let index in res[2].value.tsampleBorrowDelay){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[2].value.tsampleBorrowDelay[index])
								_that.$set(_that.formdata,index,res[2].value.tsampleBorrowDelay[index])
							}
						}
						if(_that.subEditorObj['title2']){
							let dataList = res[2].value.tsampleBorrowDelay_data
							_that.subEditorObj['title2'].workList = dataList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'dataStr',JSON.stringify(dataList))
							_that.$set(_that.formdata,'dataStr',JSON.stringify(dataList))
						}
					}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.currencyId){
							_that.$refs.autoform.formdata.currencyId = res[1].value.id
							_that.$refs.autoform.formdata.currencyName = res[1].value.codeNameUnit
						}
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}else{
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
						}	
					})
				}, options)
			},
			formSubmit() {
				let _that = this
				_that.om.formSave('sampleBorrowingDelay/addTsampleBorrowDelay',_that.formdata,_that)
			}
		},
		onUnload(){
		//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
		//#endif
		},
		onLoad(options) {
			this.initValue.userinfoId = this.userId
			this.initLoad(options)
		},
		onShow() {
			let _that = this;
			uni.$on('returnTime',function(data){
				_that.returnTime = data
				if(_that.subEditorObj['title2'].workList.length){
					for(let item of _that.subEditorObj['title2'].workList){
						item.returnDate = data
					}
				}
			})
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key:_that.relateCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							 //借样单
							formdata.relateId = d.id
							formdata.relateCode = d.code
							_that.getResult(d.code)
							//借样负责人
							formdata.borrowedSampleId = d.borrowedSampleId
							formdata.customerUserId = d.customerUserId
							formdata.customerUserName = d.customerUserName
							//岗位
							formdata.postId = d.postId
							formdata.postName = d.postName
							//机构
							formdata.instId = d.instId
							formdata.instName = d.instName
							//销售属性类别
							formdata.salesAttributeCategoryId = d.salesAttributeCategoryId
							formdata.salesAttributeCategoryName = d.salesAttributeCategoryName
							//延期次数
							formdata.productDelayNum = d.productDelayNum
							//客户信息
							formdata.customerShowId = d.customerShowId
							//客户代码
							formdata.customerId = d.customerId
							//客户名称
							formdata.customerNameX = d.customerNameX
							//收货地址
							formdata.customerAddress = d.customerAddress
							//收货联系人
							formdata.customerUser = d.customerUser
							//联系电话
							formdata.customerMobile = d.customerMobile
							//无税金额合计
							formdata.customerMoney = d.customerMoney
							//备注
							formdata.shortRemark = d.shortRemark
							uni.removeStorage({
								key:_that.relateCache
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
       